<template>
	<view class="selectDepartment" :style="{height: `${windowHeight}px`}">
		<view class="srchWrap">
			<u-input v-model="srchVal" readonly placeholder="不知道挂什么科室？" />
			<view class="seekAdviceFrom" @click="goIntelligenceAdvice">前往问询></view>
		</view>
		<!-- <view class="hostoryList" v-if="hostoryList.length>0">
		<view class="hostoryItem" v-for="(item,index) in hostoryList" :key="index">
			{{item}}
		</view>
	</view> -->
		<view class="deptList">
			<view class="deptLeft" style="height: calc(100vh - 5rem);">
				<view :class="['leftItem',avtiveIndex==index?'bgfff':'']" v-for="(item,index) in wardList" :key="index"
					@click="getDeptList(item,index)">
					<view :class="['line',avtiveIndex==index?'':'bgfff']"></view>
					<view class="title" :style="[{color:(avtiveIndex==index?'#1677FF':'#323232')}]">
						{{item.wardName}}
					</view>
				</view>
			</view>
			<view class="deptRight" style="height: calc(100vh - 5rem);">
				<view class="rightText" v-for="(item,index) in deptList" :key="index" @click="goDeptRegistration(item)">
					{{item.deptName}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		deptInfoHisWard,
		deptInfoList,
		wardInfoList
	} from '@/api/system/public'
	import {
		getConfigKey,
		getVisitorList
	} from "@/api/home/home.js";
	import {
		getQueryString,
		idCard
	} from "@/utils/common.js";
	import {
		patientBase,
		getInfoMatch
	} from '@/api/card/index.js'
	import {
		EventBus
	} from "@/utils/event-bus.js";
	export default {
		data() {
			return {
				windowHeight: uni.getSystemInfoSync().windowHeight,
				srchVal: '',
				hostoryList: [],
				avtiveIndex: 0,
				wardList: [],
				deptList: [],
				registerType: '',
				openid: '',
			}
		},
		onLoad(options) {
			let openid = uni.getStorageSync('openid') || ''
			this.openid = openid
			this.registerType = options.registerType
			if (uni.getStorageSync('hostoryList')) {
				this.hostoryList = JSON.parse(uni.getStorageSync('hostoryList'))
			}
			if (uni.getStorageSync('hospital')) {
				this.getWardList()
			}
			this.getConfigOutSideJump()
		},
		methods: {
			/* 是否开启对接外部跳转 */
			getConfigOutSideJump() {
				getConfigKey('sys_outside_link_jump').then(res => {
					if (res.msg == '1') {
						if (uni.getStorageSync('userId') != '' && this.openid) {
							this.jzswjw(uni.getStorageSync('userId'))
						}
					} else {
						uni.removeStorageSync('userId')
						uni.removeStorageSync('jzjktFunc')
					}
				});
			},
			/* 回调接口获取用户信息 */
			jzswjw(userid) {
				let _that = this
				uni.request({
					url: 'https://jkt.jzswjw.gov.cn/AREAServer/user',
					method: 'GET',
					data: {
						userId: userid
					},
					success: (res) => {
						_that.judgeIsNoCreateCard(res.data.data)
					},
					fail: () => {}
				})

			},
			/* 判断回调来的用户是否已建卡 */
			judgeIsNoCreateCard(params) {
				getVisitorList({
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					openId: this.openid
				}).then(res => {
					console.log(res, '---就诊卡列表-----')
					/* 人员列表 */
					if (res.rows.length > 0) { //用户已建卡
						EventBus.$emit('loadVisitor', '')
						uni.removeStorageSync('userId')
						uni.removeStorageSync('jzjktFunc')
					} else { //用户未建卡默认建卡
						this.autoCreatCard(params)
					}
				});
			},
			/* 自动建卡 */
			autoCreatCard(info) {
				if (uni.getStorageSync('openid')) {
					let params = {
						address: '',
						headImg: this.$store.state.user.avatar,
						hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
						idNum: info.idNo,
						mobile: info.mobile,
						name: info.name,
						nation: '',
						nickName: this.$store.state.user.name,
						openId: this.openid,
						relation: '1',
						relationName: '本人',
						sex: idCard(info.idNo, 2) == '男' ? '1' : idCard(info.idNo, 2) == '女' ? '2' : ''
					}
					getInfoMatch({
						name: info.name,
						idNum: info.idNo
					}).then(ress => {
						if (ress.data && ress.data.patientCard) {
							params.cardNum = ress.data.patientCard
							params.feeType = ress.data.mediTreatement
							params.cardStatu = ress.data.cardType
							params.feeBalance = ress.data.cardBalance
						}
						patientBase(params).then(res => {
							console.log(res, '---建卡成功呢-----')
							if (res.code == 200) {
								EventBus.$emit('loadVisitor', '')
								this.setCurrentCard()
								uni.removeStorageSync('userId')
								uni.removeStorageSync('jzjktFunc')
							}
						}).catch(errors => {});
					}).catch(errors => {});
				}
			},
			/* 设置当前卡 */
			setCurrentCard() {
				getVisitorList({
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					openId: this.openid
				}).then(res => {
					if (res.rows.length > 0) { //用户已建卡
						uni.setStorageSync('currentVisitCard', JSON.stringify(res.rows[0]))
					}
				});
			},
			doSearch() {
				if (this.hostoryList.indexOf(this.srchVal) == -1) {
					this.hostoryList.push(this.srchVal)
					uni.setStorageSync('hostoryList', JSON.stringify(this.hostoryList))
				}
			},
			goIntelligenceAdvice() {
				uni.navigateTo({
					url: '/pages/home/intelligenceAdvice'
				})
			},
			getWardList() {
				wardInfoList({
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode
				}).then(res => {
					if (res.rows.length > 0) {
						this.wardList = res.rows
						this.wardCode = this.wardList[0].wardCode
						this.getDeptList(this.wardList[0], 0)
					}
				})
			},
			getDeptList(item, index) {
				this.avtiveIndex = index
				this.wardCode = item.wardCode
				deptInfoList({
					hospitalCode: JSON.parse(uni.getStorageSync('hospital')).hospitalCode,
					wardCode: item.wardCode
				}).then(res => {
					this.deptList = res.rows
				})
			},
			goDeptRegistration(item) {
				uni.navigateTo({
					url: '/pages/home/deptRegistration?deptCode=' + item.deptCode + '&wardCode=' + this.wardCode +
						'&registerType=' + this.registerType
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #fff;
	}

	.deptList {
		display: flex;
		font-size: 1rem;

		.deptLeft {
			width: 50%;
			overflow-y: auto;

			.leftItem {
				display: flex;
				align-items: center;
				background-color: #f5f5f5;

				.line {
					width: 0.2rem;
					height: 1rem;
					background-color: #1677FF;
					margin-right: 2rem;
				}

				.title {
					padding: 0.8rem;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.bgfff {
					background: #f5f5f5;
				}
			}

			.bgfff {
				background: #fff;
			}
		}

		.deptRight {
			width: 50%;
			overflow-y: auto;

			.rightText {
				padding: 0.8rem;
				border-bottom: 1px solid #f5f5f5;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
		}
	}

	.srchWrap {
		width: 100%;
		position: relative;
		margin: 0.5rem 0;

		.seekAdviceFrom {
			color: #1677FF;
			position: absolute;
			right: 12%;
			top: 0.6rem;
			font-size: 0.8rem;
		}

		.u-input {
			width: 85%;
			margin: 0 auto;
			border-radius: 20px;
			padding-left: 20px;
			font-size: 0.8rem;
			border-width: 1px;
		}
	}

	.hostoryList {
		display: flex;
		flex-wrap: nowrap;
		overflow-x: auto;
		padding: 10px;

		.hostoryItem {
			font-size: 0.7rem;
			padding: 2px 10px;
			background: #f5f5f5;
			font-size: #666;
			border-radius: 10px;
			margin-right: 10px;
			white-space: nowrap;
		}
	}
</style>